package metamodels;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Class extends Classifier {

	Identifier identifier;
	Extensions extensions;
	Features features;
	
	public Class(Identifier identifier, Extensions extensions, Features features) {
		this.identifier = identifier;
		this.extensions = extensions;
		this.features = features;
	}

	@Override
	public String toString() {
		return "class " + identifier + extensions + " {\n" + features + "\n}";
	}
	
	@Override
	public Set<? extends String> getIdentifiers() {
		
		Set<String> ids = new HashSet<String>();
		
		ids.add(identifier.toString());
		
		for (Feature f : features.getFeatures())
			ids.addAll(f.getIdentifiers());
		
		return ids;
	}

	@Override
	public List<? extends Identifier> getIdentifiers_() {
	
		List<Identifier> ids = new ArrayList<Identifier>();
		
		ids.add(identifier);
		
		for (Feature f : features.getFeatures())
			ids.addAll(f.getIdentifiers_());
		
		return ids;
	}
	
	@Override
	public String getName()
	{
		return identifier.toString();
	}

	public boolean isParentOf(Class child) {
		
		Extensions ext = child.getExtensions();
		
		if (ext.getIdentifiers() == null)
			return false;
		
		for(Identifier i : ext.getIdentifiers())
		{
			if (i.toString().equals(identifier.toString()))
				return true;
		}
		System.out.println();
		return false;
	}
	
	public Extensions getExtensions() {
		return extensions;
	}

	@Override
	public Identifier getIdentifier() {
		return identifier;
	}

	public String getAttributeDeclRules() {
		
		String maps = "";
		
		for (Feature f : features.getFeatures())
			maps += f.getMapping() + "\n";
		
		return "rule attributeDecl(" + getName() + ") => .Map\n" + maps + "[:structural:]";
	}
}
